<?php
require("e/class/connect.php");
require("e/class/db_sql.php");
require("e/extend/wechat/wechat.config.php");
require("e/extend/wechat/wechat.class.php");
require("e/extend/wechat/wechat.fun.php");
$link   = db_connect();
$empire = new mysqlquery();
$WeiXin = new Wechat($options);

if(isset($_GET['code'])&&isset($_GET['state']))
{
	$code   = $_GET['code'];//前端传来的code值
	$result = $WeiXin->getOauthAccessToken();
	if($result===false)
	{
		echo 'errCode：'.$WeiXin->errCode.'#errMsg：'.$WeiXin->errMsg;
	}
	else
	{
		$userinfo = $WeiXin->getUserInfo($result["openid"]);
		//强制关注
		if($userinfo['subscribe']==0)
		{
			header('Location:http://'.$_SERVER["HTTP_HOST"].'/e/message/subscribeErrMsg.php');
			db_close();
			$empire=null;
			exit;
		}
		else
		{
		}
		$openid                = $result["openid"];//从返回json结果中读出openid
		$GuserInfo             = array();
		$GuserInfo['enews']    = 'ShowMemberInfo';
		$GuserInfo['username'] = $openid;
		$GuserInfo['ok']       = 'pkkgu';
		$u                     = ECMS_Infor_Curl($GuserInfo,1); //用户是否本地化
		$userid                = $u['userid'];
		$groupid               = 1;
		if(empty($u['userid'])) //注册用户信息
		{
			//$accessToken       = $result["access_token"];//从返回json结果中读出openid
			//$userInfo          = $WeiXin->getOauthUserinfo($accessToken,$openid);
			$add               = $userInfo;
			$add['ok']         = 'pkkgu';
			$add['enews']      = 'register';
			$add['groupid']    = $groupid ;
			$add['password']   = $openid.'ok';
			$add['repassword'] = $openid.'ok';
			$add['email']      = time().no_make_password(6).'@qq.com';
			$add['wxgroupid']  = $userInfo['groupid'];
			$add['username']   = $openid;
			$add['userpic']    = $userInfo['headimgurl'];
			$add['truename']   = $userInfo['nickname'];
			$ret               = ECMS_Infor_Curl($add,1);
			$userid            = $ret['userid'];
		}
		else
		{
			//更新用户信息
			$empire->query("update {$dbtbpre}enewsmemberadd set userpic='".$userinfo['headimgurl']."',truename='".addslashes(base64_encode($userinfo['nickname']))."',saytext='".date("Y-m-d H:i:s")."' where userid='$userid'");
		}
		//登录
		$cookieTime = time()+864000;
		$rnd  = make_password(20);//取得随机密码
		$usql = $empire->query("update {$dbtbpre}enewsmember set rnd='$rnd' where userid='$userid'");
		$set1 = esetcookie("mlusername",$openid,$cookieTime,0);
		$set2 = esetcookie("mluserid",$userid,$cookieTime,0);
		$set3 = esetcookie("mlgroupid",$groupid ,$cookieTime,0);
		$set4 = esetcookie("mlrnd",$rnd,$cookieTime,0);
		
		$checkpass=md5(md5($rnd.'-'.$userid.'-'.$openid.'-'.$groupid).'-#empire.cms!-'.$ecms_config['cks']['ckrndtwo']);
		$set5 = esetcookie('mlauth',$checkpass,$cookieTime);
		//echo 'http://'.$_SERVER["HTTP_HOST"].PHPSELFVAR($_GET['PHP_SELF'],1).'?'.$_SERVER['QUERY_STRING'];
		//exit;
		header('Location:http://'.$_SERVER["HTTP_HOST"].PHPSELFVAR($_GET['PHP_SELF'],1).'?'.$_SERVER['QUERY_STRING']);
		db_close();
		$empire=null;
		exit;
	}
}
else
{
	parse_str(htmlspecialchars_decode($_SERVER['QUERY_STRING']),$data);
	$callback = 'http://'.$_SERVER["HTTP_HOST"].'/oauth2.php?'.http_build_query($data);
	header("Location:".$WeiXin->getOauthRedirect($callback,'STATE','snsapi_base'));
}
db_close();
$empire=null;
exit;